package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.domain.Certificate;
import com.example.result.Result;
import com.example.service.CertificateService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 证书控制层
 * @date 2020-01-01
 */
@RestController
@RequestMapping("/api")
public class CertificateController {

    @Autowired
    CertificateService certificateService;

    //==================================证书管理===============================================

    /**
     * @param json
     * @return 添加成功则返回成功提示信息，反之则提示添加失败信息
     * @describe 添加证书信息
     */
    @RequestMapping(value = "/certificates", method = RequestMethod.POST)
    public Result addCertificate(@RequestBody String json) {

        //将JSON数据转换为Course对象实体
        Certificate certificate = GsonTool.getGsonInstance().fromJson(json, Certificate.class);

        return certificateService.addCertificate(certificate);
    }

    /**
     * @param json
     * @return 更新成功则返回成功提示信息，反之则提示更新失败信息
     * @describe 更新证书信息
     */
    @RequestMapping(value = "/certificates", method = RequestMethod.PUT)
    public Result updateCertificate(@RequestBody String json) {

        //将JSON数据转换为Course对象实体
        Certificate certificate = GsonTool.getGsonInstance().fromJson(json, Certificate.class);

        return certificateService.updateCertificate(certificate);
    }

    /**
     * @param certificateId
     * @return 删除成功则返回成功提示信息，反之则提示删除失败信息
     * @describe 根据编号删除证书信息
     */
    @RequestMapping(value = "/certificates/{certificateId}", method = RequestMethod.DELETE)
    public Result deleteCertificateById(@PathVariable("certificateId") String certificateId) {

        return certificateService.deleteCertificateById(certificateId);
    }

    /**
     * @return 以JSON格式返回所有证书信息
     * @describe 查询所有证书信息
     */
    @RequestMapping(value = "/certificates", method = RequestMethod.GET)
    public Result queryAllCertificate() {

        return certificateService.queryAllCertificate();
    }

    /**
     * @param certificateId
     * @return 以JSON格式返回该证书信息
     * @describe 根据编号查询该证书信息
     */
    @RequestMapping(value = "/certificates/{certificateId}", method = RequestMethod.GET)
    public Result queryCertificateById(@PathVariable("certificateId") String certificateId) {

        return certificateService.queryCertificateById(certificateId);
    }
}
